
//本地缓存
var myStorage = (function(){
	var setItem = function(item,msg){
		var itemVal;
		if(typeof(msg)=='object'){
			itemVal = JSON.stringify(msg);	//将object类型转换为string
		}else if(typeof(msg)=='string'){
			console.log(msg);
			itemVal = msg;
		}
		console.log(itemVal);
		return window.localStorage.setItem(item,itemVal);
	};
	var getItem = function(item){
		var getStorage = window.localStorage.getItem(item);
		return getStorage;
	};
	var clear = function(item){
		window.localStorage.clear();
	};
	return {
		setItem:setItem,
		getItem:getItem,
		clearStorage:clear
	}
})();

//显示时分秒
function countdownTime(endTime){
	var seconds = Math.floor(endTime%60);
	var minutes = Math.floor(endTime/60%60);
	var hours = Math.floor(endTime/3600);
	$('#hours').text(hours);
	$('#minutes').text(minutes);
	$('#seconds').text(seconds);
}
//转换成时分秒
function getLocalTime(ns) {
	var   hour=Math.floor(ns/60/60);     
	var   minute=Math.floor(ns/60)%60;     
	var   second=ns%60;     
	return   hour+":"+minute+":"+second;     
}


//获取地址栏中的参数
function GetQueryString(name)
{
     // 获取参数
    var url = window.location.search;
    // 正则筛选地址栏
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    // 匹配目标参数
    var result = url.substr(1).match(reg);
    //返回参数值
    return result ? decodeURIComponent(result[2]) : null;
}

//验证手机号码
function checkMobile(sMobile){
 if(!(/^1[0-9]{10}$/.test(sMobile))){
  return false; 
 }else{
 	return true;
 }
}

var textWidth = function(text){
	console.log(text);
    var sensor = $('<pre>'+ text +'</pre>').css({display: 'none'}); 
    $('body').append(sensor); 
    var width = sensor.outerWidth()+18;
    sensor.remove(); 
    return width;
};

//跳转到搜索页面
function gotoSearch(){
	location.href = 'searchPage.html';
}

//建立一個可存取到該file的url
function getObjectURL(file) {
	var url = null ;
	if (window.createObjectURL!=undefined) { // basic
		url = window.createObjectURL(file) ;
	} else if (window.URL!=undefined) { // mozilla(firefox)
		url = window.URL.createObjectURL(file) ;
	} else if (window.webkitURL!=undefined) { // webkit or chrome
		url = window.webkitURL.createObjectURL(file) ;
	}
	return url ;
}

//图片大小按比例缩放
function sizeWidthHeight(imgObj,oWidth,oHeight){
	var RATE = oWidth / oHeight;		//宽高比例
//	console.log(RATE);
	imgObj.each(function(){
		var that = $(this);
		var imgW = that.width();	//图片本身的宽度
		var imgH = that.height();	//图片本身的高度
		var rate = imgW / imgH;		//图片本身的宽高比
		if(rate>RATE){
			that.width(oWidth);
			var marginTop = -(that.height() - oHeight)/2;
			that.css('margin-top',marginTop);
//			console.log(imgW+','+imgH+','+rate);
		}else if(rate==RATE){
			that.width(oWidth);
			that.height(oHeight);
//			console.log(imgW+','+imgH+','+rate);
		}else{
			that.height(oHeight);
			var marginLeft = -(that.width() - oWidth)/2;
			that.css('margin-left',marginLeft);
//			console.log(imgW+','+imgH+','+rate);
		}
	})
}
